<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
</head>
<body>
  <input type="text" id="msg"/>
  <input type="button" value="发送" onclick="send()"/>
  <p></p>
  <script>
    var ws = new WebSocket("ws://127.0.0.1:1234");
    var p = document.querySelector('p');
    ws.onopen = function() {
      ws.send("hello");
    }
    ws.onmessage = function(res) {
      console.log(res.data)
          p.innerHTML += res.data + '<br>'; 
    }
    ws.onclose = function(e) {
      console.log('websocket closed');
    }
    ws.onerror = function(e) {
      console.log('websocket error');
    }
    var msg = document.querySelector('#msg');
    function send() {
      ws.send(msg.value);
    }
  </script>
</body>
</html>
